package com.mollytask.energyManager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mollytask.R;
import com.mollytask.base.CheckAdapter;
import com.mollytask.bean.DataCheckList;
import com.mollytask.bean.DataFill;
import com.mollytask.bean.Institution;
import com.mollytask.superadmin.DataRecordDetailActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by freshxu on 2017/5/30.
 */

public class EnergyFragment extends Fragment {

    private RecyclerView mEnergyRecycler;

    private CheckAdapter mAdapter;

    protected List<DataCheckList> mDataChecks = new ArrayList<>();

    protected List<DataFill> mDataFills = new ArrayList<>();



    private ProgressBar mProgress;

    private Button mSendBtn;





    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_energy, container, false);

        mSendBtn = (Button) view.findViewById(R.id.send);

        mProgress = (ProgressBar) view.findViewById(R.id.progress);

        mProgress.setVisibility(View.GONE);

        mSendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SendMessageActivity.class);
                startActivity(intent);
            }
        });
        initRecyclerView(view);
        initData();
        return view;
    }

    private void initRecyclerView(View view) {

        mEnergyRecycler = (RecyclerView) view.findViewById(R.id.energy_recycler);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mEnergyRecycler.setLayoutManager(manager);
        mAdapter = new CheckAdapter(R.layout.item_check, mDataChecks, false);
        mEnergyRecycler.setAdapter(mAdapter);


        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), DataRecordDetailActivity.class);
                intent.putExtra("dataFillResult", mDataFills.get(position));
                BmobQuery<Institution> query = new BmobQuery<Institution>();
                query.addWhereEqualTo("mInCode", mDataFills.get(position).getmInId());

                query.findObjects(new FindListener<Institution>() {
                    @Override
                    public void done(List<Institution> list, BmobException e) {
                        if (e == null) {
                            intent.putExtra("institution", list.get(0));
                            startActivity(intent);
                        }
                    }
                });
            }
        });
    }

    private void initData() {

        BmobQuery<DataFill> query = new BmobQuery<>();
        query.addWhereEqualTo("status", 1);
        query.order("-createdAt");
        query.findObjects(new FindListener<DataFill>() {
            @Override
            public void done(List<DataFill> list, BmobException e) {

                if (e == null) {
                    for (int i = 0; i < list.size(); i++) {
                        DataFill dataFill = list.get(i);
                        DataCheckList dataCheckList = new DataCheckList();
                        dataCheckList.setName(dataFill.getmInName());
                        String fillId = dataFill.getmFillId();
                        String data = fillId.substring(9, 13) + "年第" + fillId.substring(13, 14) + "季度";
                        dataCheckList.setData(dataFill.getmFillId());
                        dataCheckList.setData(data);
                        dataCheckList.setStatus(dataFill.getStatus().toString());
                        //添加至集合中
                        mDataChecks.add(dataCheckList);
                        mDataFills.add(dataFill);
                    }
                    if (mAdapter == null) {
                        mAdapter = new CheckAdapter(R.layout.item_check, mDataChecks, false);
                        mEnergyRecycler.setAdapter(mAdapter);
                    }
                    mAdapter.notifyDataSetChanged();
                }

            }
        });
    }




}
